package de.vocabulary.server;

import java.util.ArrayList;
import java.util.List;

import de.vocabulary.server.persistence.BerkeleyDbDatabaseSerialBinding;
import de.vocabulary.server.persistence.BerkeleyDbEnvironment;
import de.vocabulary.shared.model.Vocable;

public class VocableDatabaseWrapper {
	private static final String FOLDER = "data";

	private BerkeleyDbEnvironment dbEnv;
	private BerkeleyDbDatabaseSerialBinding db;
	
	private static VocableDatabaseWrapper vocableDatabase;
	
	private VocableDatabaseWrapper(){
		dbEnv = new BerkeleyDbEnvironment(FOLDER, false, true);
		db = new BerkeleyDbDatabaseSerialBinding(
				dbEnv, Vocable.class
		);
	}
	
	public static VocableDatabaseWrapper getVocableDatabaseWrapper(){
		if (vocableDatabase==null) vocableDatabase = new VocableDatabaseWrapper();
		return vocableDatabase;
	}
	
	public List<Vocable> getVocabularyList() throws Exception {
		List<Vocable> list = new ArrayList<Vocable>();
		
		for (String key : db.getKeys()) {
			list.add((Vocable) db.get(key));
		}
		return list;
	}
	
	//TODO rather load from memory and reduce to lesson
	public List<Vocable> getVocabularyList(String lesson) throws Exception {
		List<Vocable> list = new ArrayList<Vocable>();
		
		for (String key : db.getKeys()) {
			list.add((Vocable) db.get(key));
		}
		return list;
	}
	
	public void storeVocabularyList(List<Vocable> vocabulary) throws Exception {
		for (Vocable v: vocabulary){
			if (v.getLanguage1Word() != null) {
				db.put(v.toString(), v);
				System.out.println("Storing: "+v.toString());
			}
		}
	}
	
	// TODO
	public List<String> getLanguages() throws Exception {
		List<String> languages =  new ArrayList<String>();
		languages.add("Hebrew");
		languages.add("Turkish");
		return languages;
	}
	
	
	public List<String> getLessons() throws Exception {
		//TODO get currently loaded language and get its lessons
		List<String> lessons = new ArrayList<String>();
		lessons.add("Lesson 1");
		lessons.add("Lesson 2");
		return lessons;
	}
}
